
using System;
using System.Text;
using SpotifyGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using SpotifyGenNHibernate.EN.Spotify;
using System.Collections.Generic;

namespace SpotifyGenNHibernate.CAD.Spotify
{
        public partial class CancionCAD : BasicCAD, ICancionCAD
        {
            public CancionCAD() : base ()
            {
            }

            public CancionCAD(ISession sessionAux) : base (sessionAux)
            {
            }



            public CancionEN ReadOIDDefault (int IdCancion)
            {
                    CancionEN cancionEN = null;

                    try
                    {
                            SesssionInitializeTransaction ();
                            cancionEN = (CancionEN)session.Load (typeof(CancionEN),IdCancion);
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in CancionCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }

                    return cancionEN;
            }


            public int CrearCancion (CancionEN cancion)
            {
                    try
                    {
                            SesssionInitializeTransaction ();
                            if (cancion.Album != null) {
                                    cancion.Album = (SpotifyGenNHibernate.EN.Spotify.AlbumEN)session.Load (typeof(SpotifyGenNHibernate.EN.Spotify.AlbumEN), cancion.Album.IdAlbum);
                            }

                            session.Save (cancion);
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in CancionCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }

                    return cancion.IdCancion;
            }

            public CancionEN ObtenerDatos (int IdCancion)
            {
                    CancionEN cancionEN = null;

                    try
                    {
                            SesssionInitializeTransaction ();
                            cancionEN = (CancionEN)session.Load (typeof(CancionEN),
                                    IdCancion

                                    );
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in CancionCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }

                    return cancionEN;
            }

            public void ModificarDatos (CancionEN cancion)
            {
                    try
                    {
                            SesssionInitializeTransaction ();
                            CancionEN cancionEN = (CancionEN)session.Load (typeof(CancionEN), cancion.IdCancion);

                            cancionEN.Titulo = cancion.Titulo;


                            cancionEN.Duracion = cancion.Duracion;


                            cancionEN.Precio = cancion.Precio;


                            cancionEN.Popularidad = cancion.Popularidad;

                            session.Update (cancionEN);
                            SessionCommit ();
                    }

                    catch (Exception ex) {
                            SessionRollBack ();
                            if (ex is ModelException)
                                    throw ex;
                            throw new DataLayerException ("Error in CancionCAD.", ex);
                    }


                    finally
                    {
                            SessionClose ();
                    }
            }

             public IList<CancionEN> ObtenerCanciones()
            {
                IList<CancionEN> results = null;
                try
                {
                    SesssionInitializeTransaction();
                    results = session.CreateCriteria(typeof(CancionEN)).List<CancionEN>();
                    SessionCommit();
                    return results;
                }
                catch (Exception ex)
                {
                    SessionRollBack();
                    throw new DataLayerException("Error al cargar todas las canciones", ex);
                }
                finally 
                {
                    SessionClose();
                }
            }
        }
}
